package com.awesome.todo;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.awesome.todo.Task.Category;
import com.awesome.todo.activities.TaskMapActivity;
import com.awesome.todo.database.TaskDataSource;

public class ListViewAdapter extends BaseAdapter {

	private ArrayList<Task> tasks;

	TaskDataSource taskDataSource;
	Context context;

	public ListViewAdapter(Context context, ArrayList<Task> task) {
		tasks = task;
		this.context = context;
		taskDataSource = new TaskDataSource(context);
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return tasks.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return tasks.get(position);
	}

	public long getItemId(int index) {
		// TODO Auto-generated method stub

		return index;
	}

	public View getView(final int position, View view, ViewGroup parent) {
		LayoutInflater inflater = LayoutInflater.from(parent.getContext());
		view = inflater.inflate(R.layout.tasklistitem, null);

        // TODO - add a map button to the layout for tasklistitem. 

		TextView taskTextView = (TextView) view.findViewById(R.id.taskItemTextView);
		TextView dateTextView = (TextView) view.findViewById(R.id.dateTextView);
		TextView categoryTextView = (TextView) view.findViewById(R.id.categoryItemTextView);
        ImageView mapButton = (ImageView) view.findViewById(R.id.mapButton);
		final Task task = tasks.get(position);
		
		mapButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				final Intent intent = new Intent(v.getContext(), TaskMapActivity.class);
				intent.putExtra("address", task.getAddress());
				intent.putExtra("city", task.getCity());
				intent.putExtra("state", task.getState());
				v.getContext().startActivity(intent);
			}
			
		});
         
		Category type = task.getType();
		if (type.equals(Category.Personal)) {
			view.setBackgroundColor(context.getResources().getColor(R.color.LightBlue));
			categoryTextView.setText("Personal");
		} else if (type.equals(Category.School)) {
			view.setBackgroundColor(context.getResources().getColor(R.color.LightGreen));
			categoryTextView.setText("School");
		} else if (type.equals(Category.Work)) {
			view.setBackgroundColor(context.getResources().getColor(R.color.LightPink));
			categoryTextView.setText("Work");
		} else if (type.equals(Category.NULL)) {
			view.setBackgroundColor(context.getResources().getColor(R.color.LightYellow));
			categoryTextView.setText("None");
		}

		taskTextView.setText(task.getName());
		dateTextView.setText(task.getDate());

		CheckBox completionBox = (CheckBox) view.findViewById(R.id.completed);
		Log.i("INFO", task.getName().toString() + " is " + task.getCompleted());
		completionBox.setChecked(tasks.get(position).getCompleted());
		completionBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			public void onCheckedChanged(CompoundButton arg0, boolean checked) {
				taskDataSource.setCompletedTask(tasks.get(position).getId(), checked);
				tasks.get(position).setCompleted(checked);
				Log.i("INFO", "Set " + tasks.get(position).getName().toString() + " to "
						+ tasks.get(position).getCompleted());
			}
		});
		return view;

	}

	public void setTasks(ArrayList<Task> tasks) {
		this.tasks = tasks;
	}
	
	public ArrayList<Task> getTasks() {
		return tasks;
	}

}
